<template>
  <div>
    <el-upload
      list-type="picture-card"
      :limit="1"
      action="#"
      :on-preview="preview"
      :file-list="fileList"
      :on-remove="handleRemove"
      :on-change="changeFile"
      :before-upload="beforeUpload"
      :class="{disabled: filterShow}"
      :http-request="upload"
    >
      <i class="el-icon-plus" />
    </el-upload>
    <el-progress v-if="showPercent" :percentage="percent" style="width:180px" />
    <el-dialog title="图片" :visible.sync="showDialog">
      <img :src="imgUrl" style="width:100%" alt=""></el-dialog>
  </div>

</template>

<script>
import CORS from 'cos-js-sdk-v5'
const cors = new CORS({
  SecretId: 'AKIDjIekY6c9aooX2UO4WllaAWfkfS0INFub',
  SecretKey: 'dWFvROPBgusc7NOD6kBRSGrENKI3btCL'
})
export default {

  components: {},
  data() {
    return {
      showPercent: false,
      percent: 0,
      fileList: [], // 图片地址设置为数组
      showDialog: false, // 控制显示弹层
      imgUrl: '',
      currentFileUid: '' // 记录上传图片的id
    }
  },
  computed: {
    filterShow() {
      return this.fileList.length === 1
    }
  },
  methods: {
    preview(file) {
      // 这里应该弹出一个层 层里是点击的图片地址
      this.imgUrl = file.url
      this.showDialog = true
    },
    handleRemove(file) { // 删除所选择的文件
      this.fileList = this.fileList.filter(item => item.uid !== file.uid)
    },
    changeFile(file, fileList) { // 文件改变触发函数
      this.fileList = fileList.map(item => item)
    },
    beforeUpload(file) { // 上传文件前检查
      const types = ['image/jpeg', 'image/gif', 'image/bmp', 'image/png', 'image/webp']
      if (!types.includes(file.type)) { // 判断是否包含在其中
        this.$message.error('图片必须是JPG、GIF、BMP、PNG 格式!')
        return false
      }
      const maxSize = 1024 * 1024 * 5
      if (maxSize < file.size) { // 判断图片的大小，不能超过5mb
        this.$message.error('图片大小最大不能超过5M!')
        return false
      }
      this.currentFileUid = file.uid // 对上传成功的图片，进行标记
      this.showPercent = true
      return true
    },
    upload(params) {
      if (params.file) {
        cors.putObject({
          Bucket: 'ggbond52-1315677383', // 存储桶名称
          Region: 'ap-beijing', // 存储桶地域
          Key: params.file.name, // 文件名作为key
          StorageClass: 'STANDARD', // 此类写死
          Body: params.file, // 将本地的文件赋值给腾讯云配置
          onProgress: (res) => { // 对本地的百分比进行赋值
            this.percent = res.percent * 100
          }
        }, (err, data) => {
          if (!err && data.statusCode === 200) { // 上传成功
            // console.log(this.fileList)
            this.fileList = this.fileList.map(item => {
              if (item.uid === this.currentFileUid) { // 如果数组存在上传成功的图片，对本地的路径进行赋值
                return { url: 'http://' + data.Location, upload: true }
              }
              return item
            })
            // console.log('22', this.fileList)
            setTimeout(() => {
              this.showPercent = 0
              this.showPercent = false
            }, 1000)
          }
        })
      }
    }
  }
}
</script>

<style lang='scss' scoped>
::v-deep .disabled .el-upload--picture-card{
    display: none;
}
</style>
